Vue.prototype.$http = axios //配置axiso的全局属性
axios.defaults.baseURL = "http://localhost:8080"

//======================axios的前置拦截器【拦截调用后端的请求====================//
axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;  //
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的前置拦截器【拦截调用后端的请求】====================//

//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && "toLogin" === res.data.message) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href ="/login.html";
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//

//静态资源拦截器：如果是登录或注册页面不需要拦截。其他页面都需要判断有没有登录
let url = location.href;  //http://127.0.0.1/index.html
//如果访问的不是登录面，也不是注册页面
if(url.indexOf("/login.html")==-1 && url.indexOf("/register.html")==-1 &&
    url.indexOf("/binder.html")==-1 && url.indexOf("/callback.html")==-1){
    var token = localStorage.getItem("token");
    if(!token){
        location.href ="/login.html";
    }
}

//封装公用方法 - 将字符串的参数解析为json对象
function parseUrlParams2Obj(url) {//xxx?code=xxx&state=1
    let paramStr = url.substring(url.indexOf("?")+1);//code=xxx&state=1
    let paramArr = paramStr.split("&");//[code=xxx,state=1]
    let paramObj = {};
    for(let i = 0;i<paramArr.length;i++){
        let paramTemp = paramArr[i];//code=xxx
        let paramName = paramTemp.split("=")[0];//code
        let paramValue= paramTemp.split("=")[1];//xxx
        paramObj[paramName] = paramValue;//{code:xxx,state:1}
    }
    return paramObj;
}
